<template>
	<view>
		<view class="waterfall-wrap">
			<view>
				<view @click="jump(item)" v-for="(item, index) in leftData" :key="item.id" class="post-item">
					<u--image v-if="item.type == 1" width="100%" :src="$utils.imgPath(item.media[0])" :lazy-load="true"
						radius="10rpx">
					</u--image>
					<view v-if="item.type == 2" class="video-wrap">
						<image :lazy-load="true" :src="$utils.videoCover(item.media[0])" mode="widthFix"></image>
						<image class="pay-icon" :lazy-load="true" src="../../static/images/play.png"></image>
					</view>
					<view class="post-title">{{ item.content.substring(0, 20) }}</view>
					<view class="footer">
						<image class="avatar" :lazy-load="true" :src="item.userInfo.avatar" mode="aspectFill"></image>
						<text class="username">{{ item.userInfo.nickname.substring(0, 10) }}</text>
						<text class="thumb-num">{{ item.thumb_num }}</text>
						<u-icon class="thumb-icon" name="heart" size="36rpx"></u-icon>
					</view>
				</view>
			</view>
			<view>
				<view @click="jump(item)" v-for="(item, index) in rightData" :key="item.id" class="post-item">
					<u-image v-if="item.type == 1" width="100%" :src="$utils.imgPath(item.media[0])" :lazy-load="true"
						radius="10rpx">
					</u-image>
					<view v-if="item.type == 2" class="video-wrap">
						<image :lazy-load="true" :src="$utils.videoCover(item.media[0])" mode="widthFix"></image>
						<image class="pay-icon" :lazy-load="true" src="../../static/images/play.png"></image>
					</view>
					<view class="post-title">{{ item.content.substring(0, 20) }}</view>
					<view class="footer">
						<image class="avatar" :lazy-load="true" :src="item.userInfo.avatar" mode="aspectFill"></image>
						<text class="username">{{ item.userInfo.nickname.substring(0, 10) }}</text>
						<text class="thumb-num">{{ item.thumb_num }}</text>
						<u-icon class="thumb-icon" name="heart" size="36rpx"></u-icon>
					</view>
				</view>
			</view>
		</view>
		<!-- 加载状态 -->
		<block v-if="leftData.length === 0 && loadStatus == 'nomore'">
			<u-empty text="暂无内容" icon="/static/empty.png"></u-empty>
		</block>
		<block>
			<view style="margin-bottom: 100rpx;">
				<u-loadmore :status="loadStatus" loadingIcon='semicircle' :iconColor="themesColor" />
			</view>
		</block>
	</view>
</template>

<script>
	import {
		themesColor
	} from '@/uni.scss';

	export default {
		name: 'q-post-waterfall',
		props: {
			list: {
				type: Array,
				default: []
			},
			loadStatus: {
				type: String
			}
		},
		data() {
			return {
				themesColor,
				leftData: [],
				rightData: []
			};
		},
		watch: {
			list(value) {
				this.handelData();
			}
		},
		methods: {
			handelData() {
				let that = this;
				this.list.forEach((item, index) => {
					let i = index + 1;
					if (i % 2 == 0) {
						that.rightData.push(item);
					} else {
						that.leftData.push(item);
					}
				});
			},
			// 清空数据
			clear() {
				this.leftData = [];
				this.rightData = [];
			},
			jump(e) {
				let url;

				// 图文
				if (e.type == 1 || e.type == 4) {
					url = '/pages/post/detail?id=' + e.id;
				}

				//视频
				if (e.type == 2) {
					url = '/pages/post/video-detail?id=' + e.id;
				}

				uni.navigateTo({
					url: url
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.waterfall-wrap {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		grid-gap: 10rpx;
		padding: 0 30rpx;
	}

	.post-item {
		margin-bottom: 25rpx;
		margin-top: 25rpx;

		.cover-img {
			width: 100%;
			max-height: 600rpx;
			border-radius: 10rpx;
		}

		.video-wrap {
			position: relative;

			image {
				width: 100%;
				max-height: 600rpx;
				border-radius: 10rpx;
			}

			.pay-icon {
				position: absolute;
				right: 30rpx;
				top: 30rpx;
				width: 50rpx;
				height: 50rpx;
			}
		}

		.post-title {
			font-size: 28rpx;
			margin-bottom: 20rpx;
		}

		.footer {
			display: flex;
			align-items: center;
			font-size: 20rpx;
			color: #999;

			.avatar {
				width: 30rpx;
				height: 30rpx;
				border-radius: 50%;
				margin-right: 10rpx;
			}

			.thumb-num {
				margin-left: auto;
				margin-right: 10rpx;
			}
		}
	}
</style>
